<script setup>
import {
    Edit,Delete,Position,Upload
} from '@element-plus/icons-vue'
import {

} from '@/api/allApi.js'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
    getallbookservice,getpartpathservice,
    updatepathservice, detailbookservice, deletepathservice, lendbookservice, getallbookcategoryservice
} from '@/api/book.js'

import {getpartf5service,
    liqingpavementdamagesheetservice,
    shuinipavementdamagesheetservice
} from '@/api/allApi.js'

//控制添加分类弹窗
const dialogVisible = ref(false)
const lenddialogVisible = ref(false)
//分页条数据模型
//const pageNum = ref(1)//当前页
const total = ref(20)//总条数
//const pageSize = ref(3)//每页条数
//文章分类数据模型
const categorys = ref([
])
//用户搜索时选中的分类id
const categoryId = ref('')
const bookcategorylist = async () => {
    let result = await getallbookcategoryservice();
    categorys.value = result.data;
}
bookcategorylist();
//用户搜索时选中的发布状态
const state = ref('')
//定义变量，控制标题
const title = ref('')
const f1 =ref(false)
const f2 =ref(false)
const f3 =ref(false)
const f4 =ref(false)
//与后端交互的json数据
const pathmodel = ref({
    startPoint: '', //起点
    endPoint: '', //终点
    number: '', //班次
    startTime: null, //出发时间
    endTime: null, //到达时间
    price: '', //费用
    way:'' //交通方式
})
const sheets = ref([])
const sheetmodel = ref([])


const findmess = ref({
    finishTime:null,
    userName:null,
    pathName:null
})


const findmodel = ref({
    pageNum: 1,
    pageSize: 20,
    startPoint: '',
    endPoint:'',
    str: ''
})
const f5list = async () => {
    console.log(findmodel.value);
    let result = await getpartf5service(findmodel.value);
    console.log(result);
    total.value = result.data.total;
    sheets.value = result.data.items;
}
f5list();
//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
    findmodel.value.pageSize = size;
    f5list();
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
    findmodel.value.pageNum = num;
    f5list();
}
const cleardata = () => {
    findmodel.value.startPoint = '',
    findmodel.value.endPoint = '',
    findmodel.value.str = ''
}
//表单校验
const rules = {
    startPoint: [
        { required: true, message: '请选择出发地', trigger: 'blur' },
    ],
    endPoint: [
        { required: true, message: '请选择目的地', trigger: 'blur' },
    ],
    number: [
        { required: true, message: '请输入班次', trigger: 'blur' },
    ],
    startTime: [
        { required: true, message: '请输入出发时间', trigger: 'blur' },
    ],
    endTime: [
        { required: true, message: '请输入到达时间', trigger: 'blur' },
    ],
    price: [
        { required: true, message: '请输入费用', trigger: 'blur' },
    ],
    way: [
        { required: true, message: '请选择交通方式', trigger: 'blur' },
    ]
}
const add = () => {
    pathmodel.value.startPoint = ''
    pathmodel.value.endPoint = ''
    pathmodel.value.number = ''
    pathmodel.value.startTime = ''
    pathmodel.value.endTime = ''
    pathmodel.value.price = 0
    pathmodel.value.way = ''
    dialogVisible.value = true;
    title.value = '添加商品'
}
const addpath = async () => {
    console.log(pathmodel.value);
    let result = await addpathservice(pathmodel.value);
    if (result.code == 0) {
        ElMessage.success("添加成功")
        dialogVisible.value = false;
        pathlist();
    }
    else {
        ElMessage.error(result.message ? result.message : '添加失败')
    }
}
const updatepath = async () => {
    console.log(pathmodel.value);
    let result = await updatepathservice(pathmodel.value);
    if (result.code == 0) {
        ElMessage.success("修改成功")
        dialogVisible.value = false;
        pathlist();
    }
    else {
        ElMessage.error(result.message ? result.message : '修改失败')
    }
}

//展示编辑弹窗
const showDialog = async (row) => {
    title.value = '沥青路面损坏调查表 ( 检查面积 F：(长X宽)= m^2 )';
    console.log(row.sheetName);
    findmess.value.finishTime = row.finishTime;
    findmess.value.userName = row.userName;
    findmess.value.pathName = row.pathName;
    sheetmodel.value = []
    console.log(findmess.value);
    let f=false;
    if(row.sheetType === '水泥混凝土路面损坏调查表'){
        title.value = '水泥混凝土路面损坏调查表 ( 检查面积 F：(长X宽)= m^2 )';
        f=true;
    }
    f1.value=true;
    let result;
    if(!f) result = await liqingpavementdamagesheetservice(findmess.value);
    else result = await shuinipavementdamagesheetservice(findmess.value);
    console.log(result.data);
    if(result.code == 0 ){
        sheetmodel.value = result.data;
        for(let i=0;i<sheetmodel.value.length;i++){
            if(sheetmodel.value[i].damageArea == 0 && sheetmodel.value[i].damageArea == 0 &&sheetmodel.value[i].damageArea == 0){
                sheetmodel.value[i].damageArea =null;
                sheetmodel.value[i].damageP =null;
                sheetmodel.value[i].downCent=null;
            }
        }
    }
    else{
        ElMessage.error(result.message ? result.message : '查看失败')
    }
}


</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>路面损坏调查表</span>
                <div class="extra">
                    <!-- <el-button type="primary" @click="add">添加商品</el-button> -->
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->
        <el-form inline>
            <el-form-item label="查询条件：">
                <el-input v-model="findmodel.str" placeholder="请输入查询内容" width="200"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="f5list">搜索</el-button>
                <el-button @click="cleardata">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 文章列表 -->
        <el-table :data="sheets" style="width: 100%">
            <el-table-column label="序号" width="60" type="index"> </el-table-column>
            <el-table-column label="调查日期" prop="finishTime" width="120"> </el-table-column>
            <el-table-column label="调查人员" prop="userName" width="100"> </el-table-column>
            <el-table-column label="道路名称" prop="pathName" width="150"> </el-table-column>
            <el-table-column label="调查表类型" prop="sheetType" width="200"></el-table-column>
            <el-table-column label="起始地址" prop="startAddress" ></el-table-column>
            <el-table-column label="结束地址" prop="endAddress" ></el-table-column>
            <el-table-column label="操作" width="120">
                <template #default="{ row }">
                    <!-- <el-button v-if="row.taskName === '日常检查'" :icon="Position" circle plain @click="elsesubmit(row)"></el-button> -->
                    <el-button  plain type="primary" @click="showDialog(row)">查看详情</el-button>
                    <!-- <el-button :icon="Delete" circle plain type="danger" @click="del(row)"></el-button> -->
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 分页条 -->
        <el-pagination v-model:current-page="findmodel.pageNum" v-model:page-size="findmodel.pageSize"
            :page-sizes="[20, 30, 40, 50]" layout="jumper, total, sizes, prev, pager, next" background :total="total"
            @size-change="onSizeChange" @current-change="onCurrentChange"
            style="margin-top: 20px; justify-content: flex-end" />
    </el-card>
    <!-- 添加f1弹窗 -->
    <el-dialog v-model="f1" :title="title" width="80%">
        <el-table :data="sheetmodel" border style="width: 100%">
            <el-table-column prop="damageType" label="损坏类型" />
            <el-table-column prop="damageArea" label="损坏面积F1(m^2)"  />
            <el-table-column prop="damageP" label="损坏密度ρ=F1/F(%)" />
            <el-table-column prop="downCent" label="单项扣分值" />
            <el-table-column prop="comment" label="备注" />
        </el-table>
        <template #footer>
            <span class="dialog-footer">
                <!-- <el-button @click="f1 = false">取消</el-button>
                <el-button type="primary" @click="addf1"> 确认 </el-button> -->
            </span>
        </template>
    </el-dialog>

</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;
    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>